﻿using ETask.Api.Data;
using ETask.Api.Dtos;
using ETask.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace ETask.Api.Repositories
{
    public class TaskFileRepository : Repository<TaskFile>, ITaskFileRepository
    {
        public TaskFileRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<int> DeleteListAsync(TaskFile[] taskFiles)
        {
            _context.TaskFiles.RemoveRange(taskFiles);
            return await _context.SaveChangesAsync();
        }

        public async Task<List<TaskFileDto>> GetListByFolderIdAsync(string folderId)
        {
            var list = await _context.TaskFiles.Join(_context.Users, f => f.CreateBy, u => u.Id, (f, u) => new { f, u })
                .Where(q => q.f.FolderId == folderId)
                .OrderByDescending(q => q.f.CreatedAt)
                .Select(q => new TaskFileDto
                {
                    Id = q.f.Id,
                    FileName = q.f.FileName,
                    FileExtension = q.f.FileExtension,
                    FilePath = q.f.FilePath,
                    FileSize = q.f.FileSize,
                    FolderId = q.f.FolderId,
                    CreateBy = q.f.CreateBy,
                    CreateByName = q.u.Name,
                    CreatedAt = q.f.CreatedAt
                }).ToListAsync();
            return list;
        }

        public async Task<PagedResult<TaskFileDto>> GetListByProject(TaskFileListParams listParams)
        {
            var query = _context.TaskFiles.Join(_context.ProjectDocs, f => f.FolderId, doc => doc.FolderId, (f, doc) => new { f, doc })
                .Join(_context.Users, res => res.f.CreateBy, u => u.Id, (res, u) => new { res, u })
                .Where(q => q.res.doc.ProjectId == listParams.ProjectId);
            if (!string.IsNullOrWhiteSpace(listParams.KeyWord))
            {
                query = query.Where(q => q.res.f.OriginFileName.Contains(listParams.KeyWord));
            }
            if (listParams.BeginTime.HasValue)
            {
                query = query.Where(q => q.res.f.CreatedAt >= listParams.BeginTime.Value);
            }
            if (listParams.EndTime.HasValue)
            {
                query = query.Where(q => q.res.f.CreatedAt <= listParams.EndTime.Value);
            }
            if (listParams.FileTypes?.Any() == true)
            {
                query = query.Where(item => listParams.FileTypes.Contains(item.res.f.FileExtension));
            }
            query = query.OrderByDescending(q => q.res.f.CreatedAt);
            return await query
                .Select(q => new TaskFileDto
                {
                    Id = q.res.f.Id,
                    FileName = q.res.f.FileName,
                    FileExtension = q.res.f.FileExtension,
                    FilePath = q.res.f.FilePath,
                    FileSize = q.res.f.FileSize,
                    FolderId = q.res.f.FolderId,
                    CreateBy = q.res.f.CreateBy,
                    CreateByName = q.u.Name,
                    CreatedAt = q.res.f.CreatedAt
                }).ToPagedResultAsync(listParams.PageDto);
        }
    }
}
